/*
 * @Author: szx
 * @Date: 2021-12-03 12:34:09
 * @LastEditTime: 2021-12-04 14:19:27
 * @Description:
 * @FilePath: \leetcode\700-799\707\707.js
 */

class LinkNode {
    constructor(val, next) {
        this.val = val;
        this.next = next === undefined ? null : next;
    }
}

var MyLinkedList = function () {
    this._size = 0;
    this._tail = null;
    this._head = null;
};

/**
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {
    if (index < 0 || index >= this._size) {
        return -1;
    }
    if (index == this._size - 1) return this._tail.val;
    let cur = this._head;
    for (let i = 0; i < index; i++) {
        cur = cur.next;
    }
    return cur.val;
};

/**
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
    if (this._size == 0) {
        this._head = new LinkNode(val, null);
        this._tail = this._head;
    } else {
        const prev = new LinkNode(val, this._head);
        this._head = prev;
    }
    this._size++;
};

/**
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    if (this._size == 0) {
        this._head = new LinkNode(val, null);
        this._tail = this._head;
    } else {
        this._tail.next = new LinkNode(val, null);
        this._tail = this._tail.next;
    }
    this._size++;
};

/**
 * @param {number} index
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
    if (index <= 0) {
        this.addAtHead(val);
    } else if (index == this._size) {
        this.addAtTail(val);
    } else if (index < this._size) {
        let cur = this._head;
        for (let i = 1; i < index; i++) {
            cur = cur.next;
        }
        let tmp = new LinkNode(val, cur.next);
        cur.next = tmp;
        this._size++;
    }
};

/**
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    if (index < 0 || index >= this._size) {
        return;
    }
    let prev = new LinkNode(-1, this._head);
    for (let i = 0; i < index; i++) {
        prev = prev.next;
    }
    prev.next = prev.next.next;

    if (index == 0) this._head = prev.next;
    if (index == this._size - 1) this._tail = prev;

    this._size--;
};

MyLinkedList.prototype.out = function () {
    let cur = this._head;
    const res = [];
    while (cur) {
        res.push(cur.val);
        cur = cur.next;
    }
};

//   Your MyLinkedList object will be instantiated and called as such:
var linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(0, 2);
linkedList.addAtIndex(3, 6);
linkedList.addAtIndex(0, 4);

linkedList.deleteAtIndex(4);
// linkedList.deleteAtIndex(6);
// linkedList.deleteAtIndex(3);

linkedList.out();

// linkedList.addAtHead(7);
// linkedList.addAtTail(4);

// linkedList.addAtHead(9);

// linkedList.addAtTail(8);

// linkedList.addAtHead(6);

// linkedList.addAtHead(0);
// console.log(linkedList.get(0)); //返回2
// console.log(linkedList.get(1)); //返回2
// console.log(linkedList.get(2)); //返回2
// console.log(linkedList.get(3)); //返回2
// console.log(linkedList.get(4)); //返回2
// console.log(linkedList.get(5)); //返回2

// linkedList.addAtIndex(1, 2); //链表变为1-> 2-> 3
// console.log(linkedList.get(1)); //返回2
// linkedList.deleteAtIndex(6); //现在链表是1-> 3
// linkedList.get(1); //返回3
